package cn.tedu.charging.order.dao.repository.Impl;

import cn.tedu.charging.order.constant.OrderStatusConstant;
import cn.tedu.charging.order.dao.mapper.OrderFailMapper;
import cn.tedu.charging.order.dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

/*
* 四层模型  controller - service - repository - mapper
* 三层模型  controller - service - mapper
* */
@Slf4j
@Repository("orderRepository")
public class OrderRepositoryImpl implements OrderRepository {

    @Autowired
    private OrderSuccessMapper orderSuccessMapper;
    @Autowired
    private OrderFailMapper orderFailMapper;

    @Override
    public Integer saveSuccessOrder(ChargingBillSuccessPO chargingBillSuccessPO) {
        return orderSuccessMapper.insert(chargingBillSuccessPO);
    }

    @Override
    public List<ChargingBillSuccessPO> orderList(Integer userId) {
        QueryWrapper<ChargingBillSuccessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        orderSuccessMapper.selectList(queryWrapper);
        return Collections.emptyList();
    }

    //MyBatis-Plus 保存数据直接 Mapper.insert
    @Override
    public Integer saveFailOrder(ChargingBillFailPO chargingBillFailPO) {
        return orderFailMapper.insert(chargingBillFailPO);
    }


    /*
    * Mybatis-plus 更新数据
    *   Mapper.update(PO,UpdateWrapper)
    * */
    @Override
    public Integer updateOrderStatusFromProcess2ExceptionEnd(String orderNo) {
        //查询条件
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bill_Id",orderNo);
        updateWrapper.eq("bill_status", OrderStatusConstant.ORDER_STATUS_PROGRESS);

        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        //调整为异常(长时间未操作充电桩)
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_EXCEPTION_END);
        return orderSuccessMapper.update(chargingBillSuccessPO,updateWrapper);
    }

    //判断订单成功表中是否有数据
    /*
    * Mybatis-Plus 查询数据
    *   Mapper.selectOne (QueryWrapper<PO>) 返回数组
    *   Mapper.selectList(QueryWrapper<PO>) 返回List集合
    *
    * */
    @Override
    public ChargingBillSuccessPO getSuccess(String orderNo) {
        log.debug("判断订单成功表中是否有数据入参:{}",orderNo);
        QueryWrapper<ChargingBillSuccessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id",orderNo);
        return orderSuccessMapper.selectOne(queryWrapper);
    }

    //判断订单失败表中是佛有数据
    @Override
    public ChargingBillFailPO getFail(String orderNo) {
        log.debug("判断订单成功表中是否有数据入参:{}",orderNo);
        QueryWrapper<ChargingBillFailPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id",orderNo);
        return orderFailMapper.selectOne(queryWrapper);
    }

    //更新SuccessOrder表中的GunId
    @Override
    public void updateSuccessOrder(String orderNo, int gunId) {
        //查询条件
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bill_Id",orderNo);

        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setGunId(gunId);
        orderSuccessMapper.update(chargingBillSuccessPO,updateWrapper);
    }

    @Override
    public Integer updateOrderStatus2NormalStop(String orderNo) {
        UpdateWrapper<ChargingBillSuccessPO> updateWrapper = new UpdateWrapper<>();
        //查询订单是否为刚创建的状态
        updateWrapper.eq("bill_id",orderNo);
        updateWrapper.eq("bill_status",OrderStatusConstant.ORDER_STATUS_PROGRESS);
        //更改的数值(充电正常结束)
        ChargingBillSuccessPO chargingBillSuccessPO = new ChargingBillSuccessPO();
        chargingBillSuccessPO.setBillStatus(OrderStatusConstant.ORDER_STATUS_NORMAL_END);
        return orderSuccessMapper.update(chargingBillSuccessPO,updateWrapper);
    }
}
